package org.alvin;

public class UnicodeUtils {

    public static String unicodetoString(String unicode) {
        if (unicode == null || "".equals(unicode)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;
        while ((i = unicode.indexOf("\\u", pos)) != -1) {
            sb.append(unicode.substring(pos, i));
            if (i + 5 < unicode.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16));
            }
        }
        return sb.toString();
    }
    //编码
    public static String stringtoUnicode(String string) {
        if (string == null || "".equals(string)) {
            return null;
        }
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    public static void main(String[] args) {
        String text ="\\u6211\\u672c\\u662f\\u663e\\u8d6b\\u4e16\\u5bb6\\u7684\\u5965\\u7279\\u66fc\\uff0c\\u5374\\u88ab\\u8be1\\u8ba1\\u591a\\u7aef\\u7684\\u602a\\u517d\\u6240\\u5bb3\\uff01\\u5965\\u7279\\u66fc\\u5bb6\\u65cf\\u5f03\\u6211\\uff01\\u5965\\u7279\\u4e4b\\u7236\\u9010\\u6211\\uff01\\u751a\\u81f3\\u65ad\\u6211\\u4f3d\\u9a6c\\u5c04\\u7ebf\\uff01\\u91cd\\u751f\\u4e00\\u4e16\\uff0c\\u4eca\\u5929\\u80af\\u5fb7\\u57fa\\u75af\\u72c2\\u661f\\u671f\\u56db\\uff01\\u8c01\\u8bf7\\u6211\\u5403\\uff1f";
        String words = unicodetoString(text);
        System.out.println(words);
    }

}
